
//M: Model,通常是和数据交互的模块，比如，对题库进行增删改查
//V: view, 通常是拿到数据之后，要进行构建网页，渲染网页内容，展示给用户的(浏览器)
//C: control, 控制器，就是我们的核心业务逻辑

#include<iostream>
#include<signal.h>
#include"../common/httplib.h"
#include"./oj_control.hpp"

using namespace httplib;
using namespace ns_control;

static control *con_ptr  = nullptr;

void Recovery(int signo)
{
    con_ptr->RecoveryMachine();//上线所有主机
}

int main()
{
    signal(SIGINT,Recovery);

    Server ser;
    control con;
    //获取所有题目列表
    ser.Get("/all_question",[&con](const Request &req,Response &resp){
        std::string html;
        con.allquestion(&html);
        resp.set_content(html,"text/html;charset=utf-8");
    });


    //获取单个题目的信息
    ser.Get(R"(/question/(\d+))",[&con](const Request &req,Response &resp){
        std::string number = req.matches[1];
        std::string html;
        con.onequestion(number,&html);
        resp.set_content(html,"text/html;charset=utf-8");
    });


    //提交代码 判断对错
    ser.Post(R"(/judge/(\d+))",[&con](const Request &req,Response &resp){
        std::string number = req.matches[1];
        std::string out_json;
        con.Judge(number,req.body,&out_json);
        resp.set_content(out_json,"application/json;charset=utf-8");
    });


    ser.set_base_dir("./wwwroot");
    ser.listen("0.0.0.0",8080);
    return 0;
}